package servlet.user;

import common.Page;
import common.RoleId_roleName;
import pojo.Department;
import pojo.User;
import service.ClazzService;
import service.UserService;
import service.serviceImpl.ClazzServiceImpl;
import service.serviceImpl.UserServiceImpl;
import servlet.BaseServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017-12-10.
 */
public class UserServlet extends BaseServlet {
    int result=0;
    UserService userService=new UserServiceImpl();
    ClazzService clazzService=new ClazzServiceImpl();
    private int PAGE_SIZE=10;
    //管理所有的用户
    public String manageUser(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        //查出所有用户 然后分页

        List<User> userList= userService.getAllUser();
        Page page=new Page();
        int pageNow=page.currentPageNow(request,userList.size());
        List<User> users=userService.getCurrUser(pageNow,PAGE_SIZE);
        request.setAttribute("users",users);
        return "/jsp/user/manageUser.jsp";
    }
    //删除用户
    public String deleteUser(HttpServletRequest request,HttpServletResponse response)  throws SQLException{

        int id=Integer.parseInt(request.getParameter("id"));
        result=userService.deleteUser(id);
        return "/user?method=manageUser";
    }
    //跳转到修改用户信息界面
    public String editUser(HttpServletRequest request,HttpServletResponse response)  throws SQLException{


        int id=Integer.parseInt(request.getParameter("id"));
        User user=userService.selectUser(id);
        request.setAttribute("user",user);
        List<Department> departmentList= clazzService.getAllDepartment();
        request.setAttribute("departmentList",departmentList);
        return "jsp/user/editUser.jsp";
    }
    //修改用户信息
    public String editUserInfo(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        int id=Integer.parseInt(request.getParameter("id"));
        User user=userService.selectUser(id);
        user.setSex(request.getParameter("sex"));
        user.setDepartment(request.getParameter("department"));
        String roleName=request.getParameter("roleName");
        user.setRoleName(roleName);
        //通过一个枚举，获得角色名对应的角色id

        user.setRoleId(RoleId_roleName.getRoleId(roleName));
        user.setRealName(request.getParameter("realName"));
        user.setTl(request.getParameter("tl"));
        result=userService.updateUserInfo(user);
        return "/user?method=manageUser";
    }
    //跳转到增加员工信息页面
    public String creatUser(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        List<Department> departmentList= clazzService.getAllDepartment();
        request.setAttribute("departmentList",departmentList);
        return "jsp/user/creatUserInfo.jsp";
    }
    //    增加员工信息
    public String creatUserInfo(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        User user=new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setSex(request.getParameter("sex"));
        user.setDepartment(request.getParameter("department"));
        String roleName=request.getParameter("roleName");
        user.setRoleName(roleName);
        //通过一个枚举，获得角色名对应的角色id
        user.setRoleId(RoleId_roleName.getRoleId(roleName));
        user.setRealName(request.getParameter("realName"));
        user.setTl(request.getParameter("tl"));
        result=userService.creatUserInfo(user);
        return "/user?method=manageUser";
    }
    //验证用户名是否占用
    public void checkName(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        String username=request.getParameter("username");

        String meg= userService.checkName(username);
        if(meg.equals("true")){
            response.getWriter().print(meg);
        }
    }
}
